<?php
namespace app\controller;

use app\controller\BaseController;
use app\controller\FormValidator\StockVaccinFormValidator;
use model\CampQuery;
use model\MaladieQuery;
use model\StockVaccin;
use model\StockVaccinQuery;
use Symfony\Component\HttpFoundation\Request;

class StockVaccinController extends BaseController
{
    public function newAction(Request $request)
    {
        $session = $this->getSession();
        $flashBag = $this->getFlashBag();
         
        if($request->isMethod('POST'))
        {
            $form = $request->request->all();
            $validator = new StockVaccinFormValidator($form);
            $errors = $validator->isValid();
            if(is_bool($errors))
            {         
                $camps = CampQuery::create()->findOneById($form['camps_id']);
                $maladie = MaladieQuery::create()->findOneById($form['maladie_id']);
                
                $vaccinBase = StockVaccinQuery::create()
                        ->filterByIdmaladie($maladie->getId())
                        ->filterByIdCamp($camps->getId())
                        ->findOne();
                if($vaccinBase == null)
                {
                    $vaccin = new StockVaccin();
                    $vaccin->setIdcamp($camps->getId());
                    $vaccin->setIdMaladie($maladie->getId());
                    $vaccin->setQuantitevaccin(intval($form['quantite']) );
                    $vaccin->save();
                }
                else
                {
                   $flashBag->add('warning', 'Le stock de vaccin est deja présent pour ce camp et cette maladie');  
                   return $this->redirect($this->generateUrl('admin_home',array())); // Si il y a des paramêtres dans la route les mettre dans le tableau
                }

                //$session->set('user',$admin);
                //var_dump($this->getUser()); die('oki 4');
                
                $flashBag->add('success', 'Le stock de vaccin a été ajouté avec succès');
                
                return $this->redirect($this->generateUrl('admin_home',array())); // Si il y a des paramêtres dans la route les mettre dans le tableau
                
            }
            else
            {
                $flashBag->add('warning', "Les champs suivant ne sont pas valides ".  implode(', ', $errors));
            }
        }
        
        $camps = CampQuery::create()->find();
        $maladies = MaladieQuery::create()->find();
        
        return $this->render('admin/vaccin/newVaccin.php', 
                array('camps' => $camps,'maladies' => $maladies));
    }
             
    public function updateAction(Request $request)
    {
        $session = $this->getSession();
        $flashBag = $this->getFlashBag();
         
        if($request->isMethod('POST'))
        {
            $form = $request->request->all();
            $validator = new StockVaccinFormValidator($form);
            $errors = $validator->isValid();
            if(is_bool($errors))
            {         
                $camps = CampQuery::create()->findOneById($form['camps_id']);
                $maladie = MaladieQuery::create()->findOneById($form['maladie_id']);
                
                $vaccinBase = StockVaccinQuery::create()
                        ->filterByIdmaladie($maladie->getId())
                        ->filterByIdCamp($camps->getId())
                        ->findOne();
                
                $vaccinBase->setQuantitevaccin($vaccinBase->getQuantitevaccin() + intval($form['quantite']) );
                $vaccinBase->save();
                
                //$session->set('user',$admin);
                //var_dump($this->getUser()); die('oki 4');
                
                $flashBag->add('success', 'La quantité de vaccin a été ajouté avec succès');
                
                return $this->redirect($this->generateUrl('admin_home',array())); // Si il y a des paramêtres dans la route les mettre dans le tableau
                
            }
            else
            {
                $flashBag->add('warning', "Les champs suivant ne sont pas valides ".  implode(', ', $errors));
            }
        }
        
        $camps = CampQuery::create()->find();
        $maladies = MaladieQuery::create()->find();
        
        return $this->render('admin/vaccin/updateVaccin.php', 
                array('camps' => $camps,'maladies' => $maladies));
    }
    
    public function listAction(Request $request)
    {
        $vaccins = StockVaccinQuery::create()->orderByIdcamp()->find();
        
        return $this->render('admin/vaccin/list.php', 
                array('vaccins' => $vaccins,
                ));
    }
    
}